package cn.iocoder.yudao.module.written.exam.service.userwrittenexamrecordsdetail;

import cn.iocoder.yudao.module.written.exam.controller.app.userwrittenexamrecordsdetail.vo.AppUserWrittenExamRecordsDetailGroupRespVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecordsdetail.vo.*;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.userwrittenexamrecordsdetail.UserWrittenExamRecordsDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.written.exam.dal.mysql.userwrittenexamrecordsdetail.UserWrittenExamRecordsDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.written.exam.enums.ErrorCodeConstants.*;

/**
 * 笔试记录详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserWrittenExamRecordsDetailServiceImpl implements UserWrittenExamRecordsDetailService {

    @Resource
    private UserWrittenExamRecordsDetailMapper userWrittenExamRecordsDetailMapper;

    @Override
    public Long createUserWrittenExamRecordsDetail(UserWrittenExamRecordsDetailSaveReqVO createReqVO) {
        // 插入
        UserWrittenExamRecordsDetailDO userWrittenExamRecordsDetail = BeanUtils.toBean(createReqVO, UserWrittenExamRecordsDetailDO.class);
        userWrittenExamRecordsDetailMapper.insert(userWrittenExamRecordsDetail);
        // 返回
        return userWrittenExamRecordsDetail.getId();
    }

    @Override
    public void updateUserWrittenExamRecordsDetail(UserWrittenExamRecordsDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateUserWrittenExamRecordsDetailExists(updateReqVO.getId());
        // 更新
        UserWrittenExamRecordsDetailDO updateObj = BeanUtils.toBean(updateReqVO, UserWrittenExamRecordsDetailDO.class);
        userWrittenExamRecordsDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserWrittenExamRecordsDetail(Long id) {
        // 校验存在
        validateUserWrittenExamRecordsDetailExists(id);
        // 删除
        userWrittenExamRecordsDetailMapper.deleteById(id);
    }

    private void validateUserWrittenExamRecordsDetailExists(Long id) {
        if (userWrittenExamRecordsDetailMapper.selectById(id) == null) {
            throw exception(USER_WRITTEN_EXAM_RECORDS_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public UserWrittenExamRecordsDetailDO getUserWrittenExamRecordsDetail(Long id) {
        return userWrittenExamRecordsDetailMapper.selectById(id);
    }

    @Override
    public List<UserWrittenExamRecordsDetailDO> getUserWrittenExamRecordsDetailByRecordsId(Long recordsId) {
        return userWrittenExamRecordsDetailMapper.selectList(new LambdaQueryWrapper<UserWrittenExamRecordsDetailDO>()
                .eq(UserWrittenExamRecordsDetailDO::getRecordsId, recordsId));
    }

    @Override
    public Map<Long, AppUserWrittenExamRecordsDetailGroupRespVO> groupByRecordsId(List<Long> recordsIds) {
        Map<Long, AppUserWrittenExamRecordsDetailGroupRespVO> map = new HashMap<>();
        if (CollectionUtils.isEmpty(recordsIds)) {
            return map;
        }
        List<AppUserWrittenExamRecordsDetailGroupRespVO> appUserWrittenExamRecordsDetailGroupRespVOList = userWrittenExamRecordsDetailMapper.groupByRecordsId(recordsIds);
        for (AppUserWrittenExamRecordsDetailGroupRespVO appUserWrittenExamRecordsDetailGroupRespVO : appUserWrittenExamRecordsDetailGroupRespVOList) {
            map.put(appUserWrittenExamRecordsDetailGroupRespVO.getRecordsId(), appUserWrittenExamRecordsDetailGroupRespVO);
        }
        return map;
    }

    @Override
    public PageResult<UserWrittenExamRecordsDetailDO> getUserWrittenExamRecordsDetailPage(UserWrittenExamRecordsDetailPageReqVO pageReqVO) {
        return userWrittenExamRecordsDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public UserWrittenExamRecordsDetailDO getEndUserWrittenExamRecordsDetail(Long recordsId) {
        LambdaQueryWrapper<UserWrittenExamRecordsDetailDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserWrittenExamRecordsDetailDO::getRecordsId, recordsId)
                .orderByDesc(UserWrittenExamRecordsDetailDO::getId)
                .last("LIMIT 1");
        return userWrittenExamRecordsDetailMapper.selectOne(lambdaQueryWrapper);
    }

}